package com.evon.yardmanagement.ui.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;

import com.evon.yardmanagement.export.ImportDataManager;
import com.evon.yardmanagement.export.ImportableData;
import com.evon.yardmanagement.export.KeyBinExportableData;
import com.evon.yardmanagement.export.ParkingSlotExportableData;
import com.evon.yardmanagement.model.KeyBin;
import com.evon.yardmanagement.model.Yard;
import com.evon.yardmanagement.service.CarManagementService;
import com.evon.yardmanagement.service.KeyBinManagementService;
import com.evon.yardmanagement.service.OEMService;
import com.evon.yardmanagement.service.ParkingSlotsService;
import com.evon.yardmanagement.service.YardManagementService;
import com.evon.yardmanagement.transformer.KeyBinTransformer;
import com.evon.yardmanagement.transformer.YardTransformer;
import com.evon.yardmanagement.ui.dto.KeyBinDTO;
import com.evon.yardmanagement.ui.dto.OemDTO;
import com.evon.yardmanagement.ui.dto.YardDTO;
import com.opensymphony.xwork2.ActionSupport;

public class KeyBinDetailsAction extends ActionSupport implements ApplicationAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<KeyBinDTO> listKeyBins;
	
	private List<YardDTO> listYard = new ArrayList<YardDTO>();
	
	private int selectedYardId;
	
	private Map<String, Object> globalData;
	
	private int yardId;
	
	private File keyBinUploadFile;
	
	private List<OemDTO> oemList;
	
	private int oemType;
	
	public List<OemDTO> getOemList() {
		return oemList;
	}

	public void setOemList(List<OemDTO> oemList) {
		this.oemList = oemList;
	}

	public int getOemType() {
		return oemType;
	}

	public void setOemType(int oemType) {
		this.oemType = oemType;
	}

	public int getYardId() {
		return yardId;
	}


	public void setYardId(int yardId) {
		this.yardId = yardId;
	}


	public int getSelectedYardId() {
		return selectedYardId;
	}


	public void setSelectedYardId(int selectedYardId) {
		this.selectedYardId = selectedYardId;
	}


	public List<YardDTO> getListYard() {
		return listYard;
	}


	public void setListYard(List<YardDTO> listYard) {
		this.listYard = listYard;
	}


	public KeyBinDetailsAction (){
		listKeyBins = new ArrayList<KeyBinDTO>();	
		//listYard = CarManagementService.getAvailableYards();
		
	}
	

	public List<KeyBinDTO> getListKeyBins() {
		return listKeyBins;
	}

	public void setListKeyBins(List<KeyBinDTO> listKeyBins) {
		this.listKeyBins = listKeyBins;
	}
	
	public String populate() { 
		
		oemList = OEMService.getAllOem();
		
		// listYard = new YardManagementService().getAllYards();
		globalData.put("oemList", oemList);
		
		
		
		if (getSelectedYardId()!=0) {
			populateKeyBin();
		}
		
		
		return "populate";
	}
	
	public String populateYard(){
		oemList = (List<OemDTO>)globalData.get("oemList");
		YardManagementService service = new YardManagementService();
		
		List<Yard> yards = service.getYardsByOemId(oemType);
		YardDTO yardDTO;
		int i =0;
		for(Yard yard : yards) {
			System.out.println("nikunj");
			yardDTO = YardTransformer.transform(yard);
			listYard.add(yardDTO);			
			i++;
		}
		//listYard = new YardManagementService().getAllYards();
		globalData.put("Yards", listYard);
		return "populate";
	}
	public String populateKeyBin(){
		/*Map map = ActionContext.getContext().getSession();
		YardDTO yardDTO = (YardDTO)map.get("yard");*/
		oemList = (List<OemDTO>)globalData.get("oemList");
		listYard = (List<YardDTO>) globalData.get("Yards");
		listKeyBins = new ArrayList<KeyBinDTO>();
		int yardId = selectedYardId;
		KeyBinManagementService kbservice = new KeyBinManagementService();
		List<KeyBin> listKB = kbservice.getKeyBinByYardId(yardId);
		KeyBinDTO keyBinDTO = null;
		for (KeyBin keyBin : listKB) {
			keyBinDTO = KeyBinTransformer.transformKeyBinToDTO(keyBin);
			listKeyBins.add(keyBinDTO);
		}
		
		return "populate";
		
		
	}
	
	public void setApplication(Map<String, Object> application) {
		this.globalData = application;
	}
	
	public String executeKeyBinUpload() {
		try {
			KeyBinExportableData data = new KeyBinExportableData(null);
			data.setFileToImport(keyBinUploadFile);

			ImportDataManager<ImportableData> importManager = new ImportDataManager<ImportableData>(
					data);
			importManager.importDataFromExcel();

			new KeyBinManagementService().importKeyBins(data.getData());
			System.err.println("tEsts---"
					+ this.keyBinUploadFile.getName());
			addActionMessage("Imported Successfully");


		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public File getKeyBinUploadFile() {
		return keyBinUploadFile;
	}


	public void setKeyBinUploadFile(File keyBinUploadFile) {
		this.keyBinUploadFile = keyBinUploadFile;
	}
	
}
